<?php

namespace App\Http\Controllers;

use App\Models\Device;
use Validator;
use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
  public function store(Request $request)
  {
    $validator = Validator::make($request->all(), [
      'device_id' => 'required|numeric|exists:devices,id',
      'request_type' => 'required|numeric',
      'phone' => 'required',
      'name' => 'required',
    ]);
    if ($validator->fails()) {
      return $this->failure(-1, $validator->errors()->first());
    }
    $data = [
      'device_id' => $request->device_id,
      'request_type' => $request->request_type,
      'phone' => $request->phone,
      'name' => $request->name,
      'created_at' => date('Y-m-d H:i:s'),
    ];
    $post = Post::create($data);
    return $this->success(0, 'OK', $post);
  }
}
